#include <dirent.h> 
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include "header.h"
#include "evaluate.h"

using namespace std;

void Evaluate::makelist(int fromlist) {
	string buf1;
	string buf2;
	string qname;
	string key;
	size_t cutpos;

	ifstream in("query_result.txt");
	if (!in) {
		cout<<"no query_result.txt"<<endl;
		exit(0);
	}

	query_list.clear();
	getline(in,buf1);
	getline(in,buf1);
	cutpos=buf1.find(".dat");
	key=buf1.substr(6,(cutpos-6));
	getline(in,buf1);

	while (getline(in,buf1)){
		istringstream iss(buf1);
		iss>>buf2>>qname;
		cutpos=qname.find(".dat");
		qname=qname.substr(0, cutpos);
		query_list.push_back(qname);
	}
	
	if (fromlist) {
		string buffer;
		ifstream in("positive.txt");
		if (!in) {
			cout<<"Unable to open the positive.txt"<<endl;
			exit(1);
		}
		
		while(getline(in, buffer)) {
			size_t bgn = buffer.rfind ("/", buffer.size());
			size_t end = buffer.find (".obj");
			buffer = buffer.substr(bgn+1, end-bgn-1);
			if (buffer != key)
				positive_list.push_back(buffer);
		}		
	} else {
		DIR* pos = NULL;
		dirent* pent = NULL;
		pos = opendir("./obj/pos");
		if (pos == NULL){
			cout<<"no file in directory obj/pos/"<<endl;
			exit(1);
		}
	
		positive_list.clear();
		while (pent=readdir(pos)){
			string temp(pent->d_name);
			if (temp != "." && temp != ".."){
				size_t nmend;
				nmend=temp.find(".obj");
				temp=temp.substr(0, nmend);
				if (temp!=key)
					positive_list.push_back(temp);
			}
		}
		closedir(pos);
	}
//	for (int i=0; i<positive_list.size(); i++) {
//		cout<<positive_list[i]<<endl;
//	}
}

void Evaluate::prnumber (int ret_num, vector<float>& prpair) {
	float precision=0;
	float recall=0;
	float A=0, AB=0, AC=0;
	for (int i=0; i < min(ret_num, (int) query_list.size()); i++){
		for(int j=0; j<positive_list.size(); j++) {
			if (query_list[i]==positive_list[j]){
				A++;
				break;
			}
		}
	}

	AB=min(ret_num, (int) query_list.size());
	AC=positive_list.size();
	precision=A/(AB);
	recall=A/(AC);
	//cout<<"Pr: "<<precision;
	//cout<<", Re: "<<recall<<endl;
	prpair[0] = precision;
	prpair[1] = recall;
}

void Evaluate::prcurve (int max_num) {
	max_num = min(max_num, (int) query_list.size());
	vector<vector<float> > prnums(max_num, vector<float>(2,0));
	for (int i=0; i<max_num; i++) {
		 prnumber(i+1, prnums[i]);
		cout<<"P: "<<prnums[i][0]<<", ";
		cout<<"R: "<<prnums[i][1]<<endl;
	}

	string buffer;
	ifstream in("data/prcurve_tp.html");
	
	if (!in) {
		cout<<"Failed to plot the curve"<<endl;
		exit(0);
	}

	ofstream out("prcurve.html");
	int del=0;
	int wrt=0;
	int end=0;
	int size=0;

	while (getline(in, buffer)) {
		if (!strcmp(buffer.c_str(), "<!-- start //-->")) {
			out<<buffer<<endl;
			del=1;
		}

		if (!strcmp(buffer.c_str(), "<!-- end //-->")) {
			del=0;
			wrt=1;
		}
		if (!size && !strcmp(buffer.c_str(), "dataTable.addRows(5);")) {
			out<<"dataTable.addRows("<<prnums.size()<<");"<<endl;
			size = 1;
			continue;
		}

		if (del) {
			continue;
		}

		if (end) {
			end=0;
			out<<"<!-- end //-->"<<endl;
			wrt=0;
		}

		while (wrt>0 && wrt<=prnums.size() && !end) {
			out<<"dataTable.setValue("<<wrt-1<<", 0, "<<prnums[wrt-1][1]<<");"<<endl;
			out<<"dataTable.setValue("<<wrt-1<<", 1, "<<prnums[wrt-1][0]<<");"<<endl;
			if (wrt==prnums.size()) 
				end=1;
			++wrt;
		}

		if (!wrt && !del)
			out<<buffer<<endl;
	}

	in.close();
	out.close();
}

